package com.xinwei.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinwei.constant.Status;
import com.xinwei.dao.StudentDao;
import com.xinwei.excpetion.ManagerException;
import com.xinwei.excpetion.ManagerExceptionEnmu;
import com.xinwei.request.AcademicSituationReq;
import com.xinwei.request.BasicInfoReq;
import com.xinwei.request.DailyManagermentReq;
import com.xinwei.request.PoliticalReq;
import com.xinwei.service.StudentService;
import com.xinwei.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public PageInfo<StudentVO> getStudentList(Integer classId, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<StudentVO> studentList = studentDao.getStudentList(classId);
        if (null == studentList || studentList.size() == 0){
            throw new ManagerException(ManagerExceptionEnmu.NO_STUDENT_INFO);
        }
        PageInfo<StudentVO> pageInfo = new PageInfo<>(studentList);
        return pageInfo;
    }

    @Override
    public List<StudentVO> searchStudent(String keyWord, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        String key = new StringBuilder().append("%").append(keyWord).append("%").toString();
        List<StudentVO> studentList = studentDao.searchStudent(key);
        if (null == studentList || studentList.size() == 0){
            throw new ManagerException(ManagerExceptionEnmu.NO_STUDENT_INFO);
        }
        return studentList;
    }

    @Override
    public BasicInfoVO getBasicInfo(Integer studentId){
        BasicInfoVO basicInfo = studentDao.getBasicInfo(studentId);
        if (null == basicInfo){
            throw new ManagerException(ManagerExceptionEnmu.NO_BASIC_INFO);
        }
        return basicInfo;
    }

    @Override
    public void editBasicInfo(BasicInfoReq basicInfoReq){
        if (!(basicInfoReq.getClassName()==null || "".equals(basicInfoReq.getClassName()))) {
            try {
                basicInfoReq.setClassId(Status.ClassEnum.nameOf(basicInfoReq.getClassName()).getCode());
            } catch (Exception e) {
                e.printStackTrace();
                throw new ManagerException(ManagerExceptionEnmu.ERROR_CLASS_NAME);
            }
        }
        Integer integer = studentDao.editBasicInfo(basicInfoReq);
        if (integer == 0){
            throw new ManagerException(ManagerExceptionEnmu.UPDATE_FAILED);
        }
    }

    @Override
    public DailyManagermentVO getDailyManagerment(Integer studentId){
        DailyManagermentVO dailyManagerment = studentDao.getDailyManagerment(studentId);
        if (null == dailyManagerment){
            throw new ManagerException(ManagerExceptionEnmu.NO_STUDENT_INFO);
        }
        return dailyManagerment;
    }

    @Override
    public void editDailyManagerment(DailyManagermentReq dailyManagermentReq){
        Integer integer = studentDao.editDailyManagerment(dailyManagermentReq);
        if (integer == 0){
            throw new ManagerException(ManagerExceptionEnmu.UPDATE_FAILED);
        }
    }

    @Override
    public PoliticalVO getPolitical(Integer studentId){
        PoliticalVO political = studentDao.getPolitical(studentId);
        if (null == political){
            throw new ManagerException(ManagerExceptionEnmu.NO_STUDENT_INFO);
        }
        return political;
    }

    @Override
    public void editPolitical(PoliticalReq politicalReq){
        Integer integer = studentDao.editPolitical(politicalReq);
        if (integer == 0){
            throw new ManagerException(ManagerExceptionEnmu.UPDATE_FAILED);
        }
    }

    @Override
    public AcademicSituationVO getAcademicSituation(Integer studentId){
        AcademicSituationVO academicSituation = studentDao.getAcademicSituation(studentId);
        if (null == academicSituation){
            throw new ManagerException(ManagerExceptionEnmu.NO_STUDENT_INFO);
        }
        return academicSituation;
    }

    @Override
    public void editAcademicSituation(AcademicSituationReq academicSituationReq){
        if (studentDao.editAcademicSituation(academicSituationReq) == 0){
            throw new ManagerException(ManagerExceptionEnmu.UPDATE_FAILED);
        }
    }

    @Override
    public Double getPoint(Integer studentId, Integer term){
        return studentDao.getPoint(studentId, term);
    }
}
